package com.meihua.shellapp.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MD5Utils {

	public static String encode(String password) {
		try {
			//获取数据摘要器
			MessageDigest digest = MessageDigest.getInstance("MD5");
			//2.摘要数据,返回保存有摘要数据的byte数组
			byte[] bytes = digest.digest(password.getBytes());

			StringBuffer sb = new StringBuffer();
			//3.对每个摘要的数据进行加密操作
			for (byte b : bytes) {
				int i = b & 0xff;
				String hexString = Integer.toHexString(i);
				// System.out.println(hexString);
				if (hexString.length() == 1) {
					hexString = "0" + hexString;
				}
				//将所有的摘要数据的密文整合在一起得到总的密文
				sb.append(hexString);
			}

			String md5 = sb.toString();

			return md5;
		} catch (NoSuchAlgorithmException e) {
			// 没有此算法异常
			e.printStackTrace();
		}

		return null;
	}

	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}
}
